import 'package:flustars/flustars.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:fzcgx_app/common/adapter_utils.dart';
import 'package:fzcgx_app/common/assets_utils.dart';
import 'package:fzcgx_app/network/http.dart';
import 'package:fzcgx_app/ui/base/base_view_model.dart';
import 'package:fzcgx_app/ui/home/model/home_order_model.dart';
import 'package:fzcgx_app/ui/login/page/login_page.dart';
import 'package:fzcgx_app/ui/order/page/order_add/order_add_page.dart';
import 'package:nav_router/nav_router.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class HomeViewModel extends BaseViewModel {
  final List titles = ['找订单', '找工厂', '找公司', '找工作'];
  int currentIndex = 0;
  PageController pageController =
      PageController(initialPage: 0, keepPage: true, viewportFraction: 1.0);
  HomeOrderListModel orderModel;
  RefreshController refreshController =
      RefreshController(initialRefresh: false);
  HomeViewModel() {
    this.setState(ViewState.Loading);
  }
  void requestApi() {
    Http.instance.post<HomeOrderListModel>(
        'home/index', {'type': (this.currentIndex + 1).toString()},
        onSuccess: (data) {
          refreshController.refreshCompleted();
      if (this.currentIndex == 0) {
        if (null != data) {
          orderModel = data;
          this.setState(ViewState.Success);
        } else {
          orderModel = HomeOrderListModel();
          this.setState(ViewState.Empty);
        }
      }
    }, onError: (error) {}, onFail: (fail) {});
  }

  void updateIndex(int index) {
    this.currentIndex = index;
    this.pageController.animateToPage(currentIndex,
        duration: Duration(milliseconds: 400), curve: Curves.easeInOut);
    notifyListeners();
  }

  void showMenu(BuildContext context) {
    showDialog(
        context: context,
        barrierDismissible: true,
        builder: (ctx) {
          return Container(
            color: Colors.white.withOpacity(0.9),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.end,
              children: <Widget>[
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: <Widget>[
                    GestureDetector(
                      onTap: () {
                         pop();
                        if (SpUtil.getString('uid').isEmpty) {
                          routePush(LoginPage());
                        } else {
                          routePush(OrderAddPage()).then((value){
                            this.requestApi();
                          });
                        }
                      },
                      child: Column(
                        children: <Widget>[
                          Image.asset(
                            AssetsUtils.getAssetsImagesPath('fabu_order.png'),
                            width: AdapterUtils.getWidth(46),
                            height: AdapterUtils.getHeight(46),
                          ),
                          SizedBox(height: AdapterUtils.getHeight(4)),
                          Text(
                            '发布',
                            style: TextStyle(
                                color: Color(0xff606B79),
                                fontWeight: FontWeight.w500,
                                decoration: TextDecoration.none,
                                fontSize: AdapterUtils.getSp(12)),
                          ),
                        ],
                      ),
                    ),
                    Column(
                      children: <Widget>[
                        Image.asset(
                            AssetsUtils.getAssetsImagesPath('fabu_recruit.png'),
                            width: AdapterUtils.getWidth(46),
                            height: AdapterUtils.getHeight(46)),
                        SizedBox(height: AdapterUtils.getHeight(4)),
                        Text(
                          '发布招聘',
                          style: TextStyle(
                              color: Color(0xff606B79),
                              fontWeight: FontWeight.w500,
                              decoration: TextDecoration.none,
                              fontSize: AdapterUtils.getSp(12)),
                        ),
                      ],
                    )
                  ],
                ),
                SizedBox(height: AdapterUtils.getHeight(80)),
                GestureDetector(
                  onTap: () {
                    pop();
                  },
                  child: Container(
                      child: Icon(
                    Icons.close,
                    color: Color(0xff606B79),
                  )),
                ),
                SizedBox(height: AdapterUtils.getHeight(60))
              ],
            ),
          );
        });
  }
  void onRefresh(){
 this.requestApi();
  }
}
